#include <iostream>
#include <vector>
#include <math.h>
#include "PGPLOT_cmap.h"
#include "cpgplot.h"

using namespace std;

PGPLOT_cmap::PGPLOT_cmap() {
	cout << "PGPLOT_cmap::PGPLOT_cmap Default Constructor" << endl;
}
PGPLOT_cmap::~PGPLOT_cmap() {
	cout << "PGPLOT_cmap::PGPLOT_cmap Default Destructor" << endl;
}


PGPLOT_gray::PGPLOT_gray() {
	cout << "PGPLOT_gray::PGPLOT_gray Default Constructor" << endl;
}
PGPLOT_gray::~PGPLOT_gray() {
	cout << "PGPLOT_gray::PGPLOT_gray Default Destructor" << endl;
}


float PGPLOT_gray::set_range(float min, float max){

	float range = max-min;
	float perlevel = range/num_indices;
	return perlevel;
}

	
void PGPLOT_gray::build_table(int levels) {

	/* default gray scale */
	
	float red=1;
	float green=1;
	float blue=1;
	float position=0;

	int min_index=0;
	int max_index=0;

	vector<float> red_list;
	vector<float> green_list;
	vector<float> blue_list;
	vector<float> position_list;

	/* check how many levels our device has */

	cpgqcol(&min_index,&max_index);
	min_index = 16; /* dont mess with the originals */
	if (levels > (max_index - min_index) || levels == 0) {
		num_indices = (max_index - min_index);
		cerr << "Rescaling to " << num_indices << " levels." << endl;
	}
	else {
		num_indices = levels;
	}

	float increment = 1.0/(num_indices);
	
	for (int index=16; index <= num_indices; index++) {

		/* actual pgplot call */

		/* cpgscr(index,red,green,blue); */
		red = red-increment;
		green = green-increment;
		blue = blue-increment;
		position = position+increment;


		red_list.push_back(red);
		green_list.push_back(green);
		blue_list.push_back(blue);
		position_list.push_back(position);		

	}
	cpgctab((const float *) &position_list[0],(const float *) &red_list[0] ,(const float *) &green_list[0],(const float *) &blue_list[0],red_list.size(),1.0,0.5);


}

PGPLOT_byr::PGPLOT_byr() {
	cout << "PGPLOT_byr::PGPLOT_byr Default Constructor" << endl;
}
PGPLOT_byr::~PGPLOT_byr() {
	cout << "PGPLOT_byr::PGPLOT_byr Default Destructor" << endl;
}


float PGPLOT_byr::set_range(float min, float max){

	float range = max-min;
	float perlevel = range/num_indices;
	return perlevel;
}

	
void PGPLOT_byr::build_table(int levels) {

	/* default gray scale */
	
	float red=0;
	float green=0;
	float blue=1;
	float position=0;

	int min_index=0;

	int max_index=0;
	
	vector<float> red_list;
	vector<float> green_list;
	vector<float> blue_list;
	vector<float> position_list;
	
	/* check how many levels our device has */

	cpgqcol(&min_index,&max_index);
	if (levels > max_index+1 || levels == 0) {
		num_indices = (max_index - min_index);
		cerr << "Rescaling to " << num_indices << " levels." << endl;
	}
	else {
		num_indices = levels;
	}
	
	float increment = 4.0/num_indices;
	float levs_per_region = floorf(num_indices/4.0);
	
	for (int index=0; index <num_indices; index++) {

		/* actual pgplot call */
        if (index == 0) {
			red=0;
			green=0;
			blue=0;
		}
		// cpgscr(index,red,green,blue);
        else if (index < levs_per_region) {
		  red = 0;
		  green = green + increment;
			blue = 1;	
			
		}
		else if (index < 2*levs_per_region) {
			red=0;
			green=1;
			blue = blue-increment;
		}
		else if (index < 3*levs_per_region) {
			blue = 0;
			green = 1;
			red=red+increment;
		}
		else {
		
			red=1;
			blue=0;
			green=green-increment;
		
		}
		cout << position << "**" << red << ":" << blue << ":" << green << endl;
		if (index == 0) {
			position = 0.0;
		}
		else {
		  position = position + increment/4.0;
		}
		red_list.push_back(red);
		green_list.push_back(green);
		blue_list.push_back(blue);
		position_list.push_back(position);
		if (index == 0) {
			red=0;
			green=0;
			blue=1;
		}
	}
	cpgctab((const float *) &position_list[0],(const float *) &red_list[0] ,(const float *) &green_list[0],(const float *) &blue_list[0],red_list.size(),1.0,0.5);

}

